package com.zwz.resp.weburl;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import java.math.BigDecimal;
import java.util.*;

@Data
public class EnterpriseReportWeburl {
    // 核心字段解析
    @JsonProperty("homepageSource")
    private String homepageSource;

    @JsonProperty("domainTitle")
    private String companyName;

    @JsonProperty("legalRepresentative")
    private String legalPerson;

    @JsonProperty("regNumber")
    private String registrationNumber;

    // 网站技术参数
    @JsonProperty("websiteTechs")
    private List<String> technologies = new ArrayList<>();

    // ICP备案信息
    @JsonProperty("ICPList")
    private List<ICPRecord> icpRecords = new ArrayList<>();

    // 域名解析
    @JsonProperty("icpDomainList")
    private List<DomainInfo> domains = new ArrayList<>();

    // 业务标签
    @JsonProperty("businessTags")
    private Set<String> businessTags = new LinkedHashSet<>();

    // 动态扩展字段
    @JsonIgnore
    private Map<String, Object> additionalFields = new HashMap<>();

    @JsonAnySetter
    public void setAdditionalField(String name, Object value) {
        this.additionalFields.put(name, value);
    }

    // 嵌套结构定义
    @Data
    public static class ICPRecord {
        @JsonProperty("filingAuditTime")
        @JsonFormat(pattern = "yyyy-MM-dd")
        private Date auditDate;

        @JsonProperty("websiteRecord")
        private String icpNumber;

        @JsonProperty("domain")
        private String domainName;
    }

    @Data
    public static class DomainInfo {
        private String domain;

        @JsonProperty("domainIps")
        private List<IPAddress> ipAddresses = new ArrayList<>();

        @Data
        public static class IPAddress {
            private String ip;

            @JsonProperty("ipAddress")
            private String location;

            @JsonProperty("ipOperator")
            private String isp;
        }
    }

    // 数据清洗方法
    @JsonIgnore
    public String getCleanDomain() {
        return this.domains.stream()
                .findFirst()
                .map(d -> d.domain.replaceAll("[^a-zA-Z0-9.-]", ""))
                .orElse("");
    }

    // 核心数值转换
    @JsonIgnore
    public BigDecimal getSafeResponseTime() {
        Object time = additionalFields.get("avgResponseTime");
        try {
            return new BigDecimal(time.toString());
        } catch (Exception e) {
            return BigDecimal.ZERO;
        }
    }

    // 解析执行方法
    public static EnterpriseReportWeburl parse(String json) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(json, EnterpriseReportWeburl.class);
    }


}